//
//  ViewController.m
//  20505-老鼠练习
//
//  Created by student on 16/3/23.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
//盒子里的奶酪数量
@property (nonatomic,assign) int num;

@property (nonatomic,strong) NSLock *lock;
//老鼠一家里的奶酪数量
@property (nonatomic,assign) float num1;
//老鼠二家里的奶酪数量
@property (nonatomic,assign) float num2;
//老鼠三家里的奶酪数量
@property (nonatomic,assign) float num3;


@end


// 练习:
// 一个盒子里有100块奶酪,有个鼠洞通到盒子
// 3只老鼠,老鼠需要把奶酪叼回自己的窝里，每只老鼠搬得速度不同。(起始的时候，每只老鼠窝里有10块奶酪)
// 鼠洞很小，每一刻只能有一只老鼠在里面通行。（搬奶酪过程，线程互斥）
// 每只老鼠跑动速度不同(来回搬一次时间为0.3s 0.4s 0.5s)
// 每只老鼠搬完了要休息(0.2s)
// 老鼠发现如果不能叼奶酪，就是在窝里吃0.1块奶酪(耗费每0.1秒),然后继续尝试能不能叼
// 显示最后3只老鼠窝里的奶酪数量，如果没得吃，那只老鼠饿死。
// 线程结束条件:盒子里奶酪没了,或者饿死了。
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.num = 100;
    self.num1 = 10;
    self.num2 = 10;
    self.num3 = 10;
    self.lock = [[NSLock alloc] init];
    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(getCheese) object:NULL];
    thread1.name = @"老鼠1";
    NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(getCheese) object:NULL];
     thread2.name = @"老鼠2";
    NSThread *thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(getCheese) object:NULL];
     thread3.name = @"老鼠3";
    [thread1 start];
    [thread2 start];
    [thread3 start];
}



-(void)getCheese
{
    while (1)
    {
        float time = 0.0;
        float left = 0.0;
        if ([[NSThread currentThread].name isEqualToString:@"老鼠1"]) {
            time = 0.3;
            left = self.num1;
        }
        else if ([[NSThread currentThread].name isEqualToString:@"老鼠2"]){
            time = 0.4;
            left = self.num2;
            
        }
        else if ([[NSThread currentThread].name isEqualToString:@"老鼠3"]){
            time = 0.5;
            left = self.num3;
        }
        if (self.lock.tryLock)
        {
            if (self.num > 0 && left > 0) {
                [NSThread sleepForTimeInterval:time];
                self.num -= 1;
                left += 1;
                [self.lock unlock];
                NSLog(@"%@去搬了奶酪。盒子里还剩%d",[NSThread currentThread].name,self.num);
                [NSThread sleepForTimeInterval:0.2];
            }
            else{
                if (self.num == 0) {
                    [self.lock unlock];
                    NSLog(@"盒子空了");
                    NSLog(@"%f,%f,%f",self.num1,self.num2,self.num3);
                }
                else if(self.num1 == 0){
                    [self.lock unlock];
                    NSLog(@"%@饿死了",[NSThread currentThread].name);
                }
                [NSThread exit];
            }
        }
        else
        {
            [NSThread sleepForTimeInterval:0.1];
            left -= 0.1;
            NSLog(@"%@没有权限去搬奶酪只能吃,家里还剩%f",[NSThread currentThread].name,left);
            if (left <= 0){
                NSLog(@"%@升仙了,终于不需要为吃的发愁了",[NSThread currentThread].name);
                [NSThread exit];
            }
        }
        if ([[NSThread currentThread].name isEqualToString:@"老鼠1"]) {
            self.num1 = left;
        }
        else if ([[NSThread currentThread].name isEqualToString:@"老鼠2"]){
            self.num2 = left;
            
        }
        else if ([[NSThread currentThread].name isEqualToString:@"老鼠3"]){
            self.num3 = left;
        }

    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
